//
//  LoginViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/27.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginService.h"
#import "RootTabViewController.h"

#import "RegisterViewController.h"
#import "ResetPswViewController.h"
#import "ResObject.h"
#import "NSString+Extention.h"
#import <IQKeyboardReturnKeyHandler.h>

#import "AuthenService.h"
#import "PersonInfo.h"
#import "ReputationDetail.h"
#import "ContactInfo.h"

// 1代表debug模式 0代表发布模式
#define iS_debug 0

@interface LoginViewController ()
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}
@property (weak, nonatomic) IBOutlet UIButton *forgetPswBtn;



@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@property (weak, nonatomic) IBOutlet UITextField *usrNameField;

@property (weak, nonatomic) IBOutlet UITextField *pswField;

@property (nonatomic, strong) AuthenService *authenService;

//
@property (nonatomic, assign) BOOL isPersonAuthered;

@property (nonatomic, assign) BOOL isContactAuthered;

@property (nonatomic, assign) BOOL isXinYuAuthered;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isPersonAuthered = NO;
    self.isContactAuthered = NO;
    self.isXinYuAuthered = NO;
    
    [self.registerBtn addTarget:self
                         action:@selector(registerAction:)
               forControlEvents:UIControlEventTouchUpInside];
    [self.forgetPswBtn addTarget:self
                          action:@selector(forgetPsw:)
                forControlEvents:UIControlEventTouchUpInside];
    
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.isPersonAuthered = NO;
    self.isContactAuthered = NO;
    self.isXinYuAuthered = NO;
    
    self.navigationController.navigationBarHidden = YES;
}


#pragma mark - actions

- (void)registerAction:(UIButton *)btn
{
    RegisterViewController *myRegister = [[RegisterViewController alloc] init];
    [self presentViewController:myRegister animated:YES completion:nil];
}

- (void)forgetPsw:(UIButton *)btn
{
    ResetPswViewController *reset = [[ResetPswViewController alloc] init];
    [self presentViewController:reset animated:YES completion:nil];}

- (IBAction)loginAction:(id)sender
{
    
#if iS_debug
    self.usrNameField.text = @"13556668270";
    self.pswField.text = @"abc123";
#endif

    // 先校验
    if (![NSString valiMobile:self.usrNameField.text])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    if (self.pswField.text.length < 6 || self.pswField.text.length > 20)
    {
        [SVProgressHUD showErrorWithStatus:@"请检查密码的格式以及位数!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    __weak LoginViewController *weakSelf = self;
    [SVProgressHUD showWithStatus:@"加载中..."];
    LoginService *loginService = [[LoginService alloc] init];
    [loginService loginWithPhone:self.usrNameField.text
                             pwd:self.pswField.text
                    withCallBack:^(ResObject *resObj) {
                        [SVProgressHUD dismiss];
                       
                        [[NSUserDefaults standardUserDefaults] setObject:self.usrNameField.text forKey:kPhoneNum];
                        
                        if (resObj.isSuccess)
                        {
                            // 联系人信息
                            [weakSelf getContactAuthenInfo];
                        }
                        else
                        {
                            [SVProgressHUD showErrorWithStatus:resObj.errMsg];
                            [SVProgressHUD dismissWithDelay:2.0f];
                        }
                    }];
}

- (void)loadMainTabbar
{
    RootTabViewController *rootVC = [[RootTabViewController alloc] init];
    rootVC.tabBar.translucent = YES;
    [self presentViewController:rootVC animated:YES completion:nil];
}

#pragma mark - 收起键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - #######actions######
- (void)getPersonAuthenInfo
{
    [self.authenService getUserInfoWithCallBack:^(PersonInfo *person) {
        if (!isEmptyString(person.name)
            && !isEmptyString(person.idCard)
            && !isEmptyString(person.school)
            && !isEmptyString(person.schoolAddr))
        {
            NSLog(@"kPersonInfoAuthenRes ==> YES");
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kPersonInfoAuthenRes];
        }
        else
        {
            NSLog(@"kPersonInfoAuthenRes ==> NO");

            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kPersonInfoAuthenRes];
        }
        
        self.isPersonAuthered = YES;
        [self getAutherInfo];
        
        // 查询信誉认证
        [self getXinYuInfo];
    }];
}

- (void)getXinYuInfo
{
    [self.authenService getReputationDetailWithCallBack:^(ReputationDetail *rep) {
        if (!isEmptyString(rep.jdAccount)
            )
        {
            NSLog(@"kXinYuAuthenRes ==> YES");

            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kXinYuAuthenRes];
        }
        else
        {
            NSLog(@"kXinYuAuthenRes ==> NO");

            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kXinYuAuthenRes];
        }
        self.isXinYuAuthered = YES;
        [self getAutherInfo];
    }];
}

- (void)getContactAuthenInfo
{
    [self.authenService getContactsWithCallBack:^(ContactInfo *info) {
        if (!isEmptyString(info.contactName1)
            && !isEmptyString(info.contactName2))
        {
            NSLog(@"kContactAuthenRes ==> YES");

            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kContactAuthenRes];
        }
        else
        {
            NSLog(@"kContactAuthenRes ==> NO");

            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kContactAuthenRes];
        }
        
        self.isContactAuthered = YES;
        [self getAutherInfo];
        
        // 查询个人认证信息
        [self getPersonAuthenInfo];
    }];
}

- (void)getAutherInfo
{
    if (self.isPersonAuthered
        && self.isContactAuthered
        && self.isXinYuAuthered)
    {
        [self loadMainTabbar];
    }
    
}

- (AuthenService *)authenService
{
    if (!_authenService)
    {
        _authenService = [[AuthenService alloc] init];
    }
    return _authenService;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
